<?php
namespace Admin\Controller;

class AreaController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $keyword = I('get.keyword');
            $where = array();
            if ($keyword) {
                $where['area_name'] = array( 'like', "%{$keyword}%" );
            }
            $where['status'] = array( 'gt' , 0 );
            $list = $this->lists(M('Area'), true, $where, 'area_id DESC', 10);
            $this->assign('list', $list);
            $this->assign('keyword', $keyword);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.');
            if (empty($data['region_list'])){
                $this->error('请配置楼宇');
            }
            $area = D('Area');
            $result = $area->update($data, $data['region_list']);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $area->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $region = getRegion(false);
            $this->assign('region', $region);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            if (empty($data['region_list'])){
                $this->error('请配置楼宇');
            }
            $area = D('Area');
            $result = $area->update($data, $data['region_list']);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $area->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('Area')->where('area_id = ' . $id)->find();
            $rela = M('AreaRegion')->where('area_id = ' . $id)->select();
            $region = getRegion(false);
            $this->assign('region', $region);
            $this->assign('rela', $rela);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'area_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Area')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    } 
    
}